<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>产品管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
		$(document).ready(function() {
			$("#name").focus();
			$("#inputForm").validate({
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
			
			$('#category_id option').each(function(n,item){ 
				pid = item.value.split('-');
				if(pid[0] === '${product.category}'){
					$(this).attr('selected','selected');
				}
				
			}); 
			
			 changeCategory($('#category_id').val());
			 
			wrapUrl();
			
		});
		
		
		function wrapUrl(){
		var preUrl = "http://www.hdzhx.com/zxims/f/wechat/product/";
			var  category = $("#category_id").val();
			var  subCategory = $("#subcategory_id").val();
			if(category==='') category = '0';
			if(subCategory==='' || subCategory == null) subCategory = '0';
			
			var pid = category.split('-');
			if(pid.length=2){
				category = pid[0];
			}
			
			preUrl = preUrl + "?category="+category+"&subCategory="+subCategory;
			$('#link_url').text(preUrl);
		}
		
		
		function changeCategory(parentId){
			var pid = parentId.split('-');
			parentId = pid[1];
			$('#category_v').val(pid[0]);
			$.post('${ctx}/wechat/productCategory/child/?id='+parentId,function(data){
				var str = "<option value='0'  >请选择</option>";
				var strselect = "";
				$.each(data,function(i,item){
					var strselect = "";
					 if(item.value == '${product.subCategory}'){
						 strselect = "selected='selected'";
					 }
					 str+="<option value='"+item.value+"' "+ strselect +">"+item.name+"</option>" ; 
				});
				$('#subcategory_id').html(str);
				wrapUrl();
			},'json');
		}
		
		
		function beforeSubmit(){
			var pid = $('#category_id').val();
			var  pv = pid.split('-');
			 parentId = pv[0];
			 if(parentId ==''){
				 alert("请选择类别,主类别不能为 '共有类别'!");
				 return false;
			 }
			$('#category_v').val(parentId);
			
			return true;
		}
		  
		
	</script>
</head>
<body>
	<div id="mydiv"></div>
	<ul class="nav nav-tabs">
		<li><a href="${ctx}/wechat/product/">产品列表</a></li>
		<li class="active"><a href="${ctx}/wechat/product/form?id=${product.id}">产品<shiro:hasPermission name="wechat:product:edit">${not empty product.id?'修改':'添加'}</shiro:hasPermission><shiro:lacksPermission name="wechat:product:edit">查看</shiro:lacksPermission></a></li>
	</ul><br/>
	<form:form id="inputForm" modelAttribute="product" action="${ctx}/wechat/product/save" method="post"   onsubmit="return beforeSubmit();" class="form-horizontal" enctype="multipart/form-data" >
		<form:hidden path="id"/>
		<form:hidden path="user.id"/>
		<form:hidden path="category" id="category_v"/>
		<tags:message content="${message}"/>
		<div class="control-group">
			<label class="control-label">连接地址:</label>
			<div class="controls">
				 <label id="link_url"></label> &nbsp; &nbsp;&nbsp;&nbsp;<a class="btn btn-success" onclick="wrapUrl()">刷新URL</a>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">名称:</label>
			<div class="controls">
				<form:input path="name" htmlEscape="false" maxlength="11" class="required"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">价格:</label>
			<div class="controls">
				<form:input path="price" htmlEscape="false" maxlength="150"  />
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">原价格:</label>
			<div class="controls">
				<form:input path="sourcePrice" htmlEscape="false" maxlength="150"  />
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">分类:</label>
			<div class="controls">
				<select name="categoryList" id="category_id" onchange="changeCategory(this.value)">
					<c:forEach items="${categoryList }" var="category" >
						<option value="${category.value}-${category.id}"  <c:if test="${productCategory.parent.id eq category.id} ">selected</c:if> >${category.name }</option>
					</c:forEach>
		</select>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">子分类:</label>
			<div class="controls">
					<form:select path="subCategory" id="subcategory_id"  onchange="wrapUrl();" >
					</form:select>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">URL:</label>
			<div class="controls">
				<form:input path="url" htmlEscape="false" maxlength="150"  />
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">显示顺序:</label>
			<div class="controls">
				<form:input path="sort" htmlEscape="false" maxlength="5"  />	<span class="alert">数值越大排序越靠前</span>	
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">产品数量:</label>
			<div class="controls">
				<form:input path="amount" htmlEscape="false" maxlength="150"  />
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">发布:</label>
			<div class="controls">
				<form:radiobutton path="isSale"/> 发布       <form:radiobutton path="isSale"/> 不发布  
			</div>
		</div>
		
		<div class="control-group">
			<label class="control-label">简介:</label>
			<div class="controls">
				<form:textarea path="description" htmlEscape="false"  cols="6" rows="8" />
			</div>
		</div>
		
			<div class="control-group">
			<label class="control-label">详细:</label>
			<div class="controls">
				<form:textarea id="detail" htmlEscape="true"  path="detail" rows="4" maxlength="200" class="input-xxlarge required"/>
				<tags:ckeditor replace="detail" uploadPath="/cms/article" />
			</div>
		</div>
		
		
		
		<div class="control-group">
			<label class="control-label">展示图片:</label>
			<div class="controls">
				<input type="file"	name="picfile"/>	<span class="alert">推荐尺寸294*145</span>		
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">产品图片1:</label>
			<div class="controls">
				<input type="file"	name="productPic1"/>			
			</div>
		</div>
		
		<div class="control-group">
			<label class="control-label">产品图片2:</label>
			<div class="controls">
				<input type="file"	name="productPic2"/>			
			</div>
		</div>
		
		<div class="control-group">
			<label class="control-label">产品图片3:</label>
			<div class="controls">
				<input type="file"	name="productPic3"/>			
			</div>
		</div>
		
		
		<div class="form-actions">
			<shiro:hasPermission name="wechat:product:edit"><input id="btnSubmit" class="btn btn-primary" type="submit"   value="保 存"/>&nbsp;</shiro:hasPermission>
			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
		</div>
	</form:form>
</body>
</html>
